//
//  FoodVideoViewController.m
//  FoodFamily
//
//  Created by Bang-guo on 15-6-19.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "FoodVideoViewController.h"

@interface FoodVideoViewController ()

@end

@implementation FoodVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //设置navigationBar背景颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"690EFC73-E213-4C10-9FF7-CCE713AC7BC1.png"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];
    
    [[FoodVideoSql shareFoodViedoSql]OpenDB];//打开数据库
    [[FoodVideoSql shareFoodViedoSql]creatTable];//创建表
    UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, WIDTH, 20)];
    statusBarView.backgroundColor=[UIColor blackColor];
    [self.navigationController.navigationBar addSubview:statusBarView];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
    [statusBarView release];
    self.navigationItem.title = @"美食教学视频";
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    
    //    self.mytableview = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-113) style:UITableViewStylePlain];
    [self.view addSubview:self.mytableview];
    [self.mytableview release];
    
    self.mytableview.delegate = self;
    self.mytableview.dataSource = self;
    self.mytableview.pagingEnabled = YES; //按页滑动，一页一页翻
    //scrollView自适应
   // [self setAutomaticallyAdjustsScrollViewInsets:NO];
    //imageView自动调整尺寸
    
    
    //禁止边界反弹效果
    //self.mytableview.bounces = NO;
    self.allarray = [NSMutableArray array];
    

#warning 初始化 self.nextPage
    self.nextPage = 1;
     [self AFN];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];
    //下来刷新  上拉加载
    [self.mytableview.header beginRefreshing];
    [self headerRefesh];
    [self footerRefesh];


   
};

#warning   下拉刷新
-(void)headerRefesh{
    self.mytableview.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.nextPage = 1;
        self.downUpdata = YES;
        
        //让菊花旋转起来
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中...";
        [self.HUD show:YES];
        [self AFN];
        
        // 刷新表格
        [self.mytableview reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        
        }];
    
}
#warning 上拉加载
-(void)footerRefesh {
    
#pragma 刷新
    self.mytableview.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        

        self.nextPage++;
        self.downUpdata = NO;
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中...";
        [self.HUD show:YES];
        [self AFN];
        
        // 刷新表格
        [self.mytableview reloadData];
        
    }];
    
}

-(void)AFN{
    //    设置状态栏的背景颜色
    NSString *string = [NSString stringWithFormat:@"http://121.41.88.194:80/HandheldKitchen/api/more/tblcalendaralertinfo!getHomePage.do?phonetype=0&page=%ld&pageRecord=9&user_id=&is_traditional=0",self.nextPage];
    if (self.nextPage==1) {
        
        NSMutableArray *foodarray = [[FoodVideoSql shareFoodViedoSql]selectallMovie];//chaxun
        self.allarray = foodarray;
        
    }
    
    
    
    
    [AFNGetDataValue getDataAFN:string block:^(NSMutableDictionary *dataDic) {
        
        self.allDic = [NSMutableDictionary dictionary];
        self.allDic = [dataDic objectForKey:@"bean"];
        if (dataDic ==nil) {
            
        }else
        {
            if (self.nextPage ==1) {
                [[FoodVideoSql shareFoodViedoSql]OpenDB];
                [[FoodVideoSql shareFoodViedoSql]dropTable];
                [self.allarray removeAllObjects];
                [[FoodVideoSql shareFoodViedoSql]creatTable];
            }
            NSMutableArray *array = [dataDic objectForKey:@"data"];
            /*@property(nonatomic,copy)NSString *imagePathLandscape;//食物图片**
             @property(nonatomic,copy)NSString *englishName;//食物名字**
             @property(nonatomic,copy)NSString *effect;//食物对身体的好处**
             @property(nonatomic,copy)NSString *name;//食物名字**
             @property(nonatomic,copy)NSString *fittingRestriction;//原料
             @property(nonatomic,copy)NSString *fittingCrowd; //适合人群
             @property(nonatomic,copy)NSString *cookingMethod ;//煮
             @property(nonatomic,copy)NSString *taste; //口味
             @property(nonatomic,copy)NSString *cookingWay ;//烹饪时间*/
            for (NSMutableDictionary *dic in array) {
                self.movie = [[Movie alloc]init];
                self.movie.imagePathLandscape = [dic objectForKey:@"imagePathLandscape"];
                self.movie.englishName = [dic objectForKey:@"englishName"];
                self.movie.effect = [dic objectForKey:@"effect"];
                self.movie.name = [dic objectForKey:@"name"];
                self.movie.fittingRestriction = [dic objectForKey:@"fittingRestriction"];
                self.movie.fittingCrowd = [dic objectForKey:@"fittingCrowd"];
                self.movie.cookingMethod = [dic objectForKey:@"cookingMethod"];
                self.movie.cookingWay = [dic objectForKey:@"cookingWay"];
                self.movie.taste  = [dic objectForKey:@"taste"];
                self.movie.timeLength = [NSString stringWithFormat:@"%@",[dic objectForKey:@"timeLength"]];
                self.movie.method = [dic objectForKey:@"method"];
                self.movie.vegetable_id  = [dic objectForKey:@"vegetable_id"];
                self.movie.materialVideoPath = [dic objectForKey:@"materialVideoPath"];
                self.movie.productionProcessPath = [dic objectForKey:@"productionProcessPath"];
                [self.allarray addObject:self.movie];
                [[FoodVideoSql shareFoodViedoSql]insertMovie:self.movie];//插入数据
                [self.movie release];
                
            }
            
        }
        /*+(instancetype)shareFoodViedoSql;
         -(void)OpenDB;//打开数据库
         -(void)creatTable;//创建表
         -(void)insertMovie:(Movie *)mov;//插入数据
         -(NSMutableArray *)selectallMovie;//查询所有
         -(void)dropTable;//删除表
         */
       // NSLog(@"111111 = %ld",self.allarray.count);
        [self.mytableview reloadData];//shuaxin table
        
#warning 下来刷新  上拉加载 结束控件
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.mytableview.footer endRefreshing];//底部
        [self.mytableview.header endRefreshing];//头部
        //[[FoodVideoSql shareFoodViedoSql]dropTable];
        [self.HUD hide:YES];
    }];
    
    
    
    
    
    
    
    
    
    
}
#pragma 点击cell触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{FoodDetailsViewController *food = [[FoodDetailsViewController alloc]init];
    Movie *movie = [self.allarray objectAtIndex:indexPath.row];
    
    food.movie =movie;
   // NSLog(@"///////%@",food.movie);
    [self.navigationController pushViewController:food animated:YES];
}
#pragma tablevie中有多少个section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}
#pragma tableview中有多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allarray.count;
}
#pragma 自定义cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *cellidentifer = @"cell";
    ViedoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifer];
    if (cell ==nil) {
        cell = [[ViedoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifer];
    }
    
    Movie *movie = [self.allarray objectAtIndex:indexPath.row];//1
    cell.mov = movie;
    
    
    //NSLog(@"11%@",movie);
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return HEIGHT-20-44-49;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
